package com.boyinet.imesh2json.fx.controller

import com.boyinet.imesh2json.bean.Template
import com.boyinet.imesh2json.fx.view.TemplateView
import com.boyinet.imesh2json.fx.view.TypeView
import com.boyinet.imesh2json.viewmodel.IndexViewModel
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport
import de.felixroske.jfxsupport.FXMLController
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.*
import javafx.scene.layout.AnchorPane
import javafx.scene.layout.HBox
import javafx.stage.Modality
import javafx.util.StringConverter
import jfxtras.styles.jmetro.FlatAlert
import jfxtras.styles.jmetro.JMetro
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL
import java.util.*

@FXMLController
class IndexViewController : Initializable {

    @FXML
    lateinit var root: AnchorPane

    @FXML
    lateinit var types: HBox

    @FXML
    lateinit var templateIds: ComboBox<Template>

    @FXML
    lateinit var port: TextField

    @FXML
    lateinit var url: TextField

    @FXML
    lateinit var listenButton: Button

    @Autowired
    lateinit var indexViewModel: IndexViewModel

    val toggleGroup = ToggleGroup()

    @FXML
    lateinit var typeItem: MenuItem

    @FXML
    lateinit var templateItem: MenuItem

    private val log: Logger = LoggerFactory.getLogger(IndexViewController::class.java)


    override fun initialize(location: URL?, resources: ResourceBundle?) {
        val jMetro = JMetro()
        jMetro.parent = root

        val toggles = types.children.map { it as RadioButton }.toList()
        toggleGroup.toggles.addAll(toggles)
        toggleGroup.selectToggle(toggleGroup.toggles[indexViewModel.current.type.value])

        renderComboBox()

        bindFormProperty()

        templateItem.setOnAction {
            AbstractJavaFxApplicationSupport.showView(TemplateView::class.java, Modality.APPLICATION_MODAL)
        }

        typeItem.setOnAction {
            AbstractJavaFxApplicationSupport.showView(TypeView::class.java, Modality.APPLICATION_MODAL)
        }


    }

    private fun renderComboBox() {
        with(templateIds) {
            items = indexViewModel.templateItems

            converterProperty().set(object : StringConverter<Template>() {
                override fun toString(item: Template?): String {
                    return item?.name!!
                }

                override fun fromString(string: String?): Template {
                    return items.asSequence().filter { i -> i.name == string }.first()
                }
            })
        }
    }

    private fun bindFormProperty() {
        port.textProperty().bindBidirectional(indexViewModel.current.port)
        url.textProperty().bindBidirectional(indexViewModel.current.url)
        listenButton.textProperty().bindBidirectional(indexViewModel.statusStr)
    }

    fun onListen() {
        log.info("onListen click")
        if (indexViewModel.status) {
            indexViewModel.stopServer()
        } else {
            if (templateIds.selectionModel.isEmpty) {
                val flatAlert = FlatAlert(Alert.AlertType.WARNING)
                flatAlert.contentText = "请先选择要转发模板！！！"
                JMetro().scene = flatAlert.dialogPane.scene
                flatAlert.show()
                return
            }
            indexViewModel.current.apply {
                type.value = toggleGroup.toggles.indexOf(toggleGroup.selectedToggle)
                templateId.value = templateIds.selectionModel.selectedItem.id
            }
            indexViewModel.startServer()
        }
    }
}